//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.pc_rfid.api;

import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.util.Log;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Vector;

public class PC_API {
    public static final int NOLINK = -1;
    public static final int USB = 0;
    public static final int COM = 1;
    public static final int NET = 2;
    private static final String TAG = "PC_API";
    private PC_COMM comm = null;
    private int LinkType = -1;
    private static byte PC_SOF = -86;

    public PC_API() {
    }

    private boolean crcAndSend_Receive(RFdata rfdata) {
        int crc = PC_COMM.crc16(rfdata.SendData);
        rfdata.SendData[rfdata.SendData[1] - 1] = (byte)(crc % 256);
        rfdata.SendData[rfdata.SendData[1]] = (byte)(crc / 256);

        String temp = HexToStr(rfdata.SendData,rfdata.SendData[1] + 1,true);
        Log.e(TAG, "temp: " + temp);
        //AA 12 FF CB 02 C0 A8 03 DE FF FF FF 00 C0 A8 03 01 00
        if (this.comm != null && this.LinkType != -1) {
            int sign = this.comm.Send_Receive(rfdata);
            if (sign > 0) {
                return true;
            }
        }

        return false;
    }

    public static String[] GetAllCOM() {
        SerialPortFinder allCOM = new SerialPortFinder();
        return allCOM.getAllDevicesPath();
    }

    public int GetLinkType() {
        return this.LinkType;
    }

    public boolean PC_OpenCOM(String portNum, int baud, String format) {
        if (this.LinkType != -1) {
            this.comm.close();
            this.LinkType = -1;
        }

        this.comm = new PC_COM(portNum, baud, format);
        if (this.comm.isOpen) {
            this.LinkType = 1;
            return true;
        } else {
            return false;
        }
    }

    public static UsbDevice[] ScanUSB(int VID, int PID) {
        return PC_USB.scanUSB(VID, PID);
    }

    public boolean PC_OpenUSB(int vid, int pid) {
        if (vid == 65534 && (pid == 145 || pid == 146)) {
            UsbDevice[] list_dev = ScanUSB(vid, pid);
            return list_dev != null ? this.PC_OpenUSB(list_dev[0]) : false;
        } else {
            return false;
        }
    }

    public boolean PC_OpenUSB(UsbDevice dev) {
        if (this.LinkType != -1) {
            this.comm.close();
            this.LinkType = -1;
        }

        this.comm = new PC_USB(dev);
        if (this.comm.isOpen) {
            this.LinkType = 0;
            return true;
        } else {
            return false;
        }
    }

    public boolean PC_OpenUSB() {
        return this.PC_OpenUSB(65534, 145);
    }

    public static void USB_init(Context ctx) {
        PC_USB.init(ctx);
    }

    public static void USB_exit() {
        PC_USB.exit();
    }

    public static void setOnUsbListener(OnUsbListener Listener) {
        PC_USB.setOnUsbListener(Listener);
    }

    public boolean PC_OpenNET(String ip, int port) {
        if (this.LinkType != -1) {
            this.comm.close();
            this.LinkType = -1;
        }

        try {
            this.comm = new PC_NET(ip, port);
        } catch (SocketException var4) {
            var4.printStackTrace();
        } catch (UnknownHostException var5) {
            var5.printStackTrace();
        }

        if (this.comm.isOpen) {
            this.LinkType = 2;
            return true;
        } else {
            return false;
        }
    }

    public boolean PC_Close() {
        if (this.comm != null && this.LinkType != -1) {
            int sign = this.comm.close();
            if (sign == 0) {
                this.LinkType = -1;
                return true;
            }
        }

        return false;
    }

    public synchronized boolean PC_Send(RFdata rfdata) {
        if (this.LinkType == -1) {
            return false;
        } else {
            int sign = this.comm.Send(rfdata.SendData);
            return sign == 0;
        }
    }

    public synchronized boolean PC_Receive(RFdata rfdata) {
        if (this.LinkType == -1) {
            return false;
        } else {
            int sign = this.comm.Receive(rfdata.RecvData);
            return sign > 0;
        }
    }

    public boolean PC_OpenDevice(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = 120;
        rfdata.SendData[4] = 1;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_InventoryTag(RFdata rfdata, byte AntOneFlag) {
        if (this.LinkType == 2) {
            return false;
        } else {
            rfdata.clear();
            rfdata.SendData[0] = PC_SOF;
            rfdata.SendData[1] = 7;
            rfdata.SendData[2] = -1;
            rfdata.SendData[3] = -2;
            rfdata.SendData[4] = 1;
            rfdata.SendData[5] = AntOneFlag;
            return this.crcAndSend_Receive(rfdata);
        }
    }

    public synchronized boolean PC_NETinventoryTag(Vector<byte[]> Rece, RFdata rfdata, byte AntOneFlag) {
        if (this.LinkType != 2) {
            return false;
        } else {
            PC_NET net = (PC_NET)this.comm;
            rfdata.clear();
            rfdata.SendData[0] = PC_SOF;
            rfdata.SendData[1] = 7;
            rfdata.SendData[2] = -1;
            rfdata.SendData[3] = -2;
            rfdata.SendData[4] = 1;
            rfdata.SendData[5] = AntOneFlag;
            int crc = PC_COMM.crc16(rfdata.SendData);
            rfdata.SendData[rfdata.SendData[1] - 1] = (byte)(crc % 256);
            rfdata.SendData[rfdata.SendData[1]] = (byte)(crc / 256);
            return net.NETinventoryTag(Rece, rfdata.SendData) > 0;
        }
    }

    public boolean PC_NoiseCheck(RFdata rfdata, byte data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -14;
        rfdata.SendData[4] = 6;
        rfdata.SendData[5] = data;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_WriteCfgBlock(RFdata rfdata, byte[] data, byte block) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 15;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -91;
        rfdata.SendData[4] = 1;
        rfdata.SendData[5] = block;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = data[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ReadCfgBlock(RFdata rfdata, byte block) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -91;
        rfdata.SendData[4] = 0;
        rfdata.SendData[5] = block;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_SaveCfgBlock(RFdata rfdata, byte block) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -91;
        rfdata.SendData[4] = 2;
        rfdata.SendData[5] = block;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_RestartCfgBlock(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -128;
        rfdata.SendData[4] = 1;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_GetDeviceInfoVersion(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = 120;
        rfdata.SendData[4] = 0;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_Open_CloseRFPower(RFdata rfdata, byte data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = 121;
        rfdata.SendData[4] = 1;
        rfdata.SendData[5] = data;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_GetOneTagInfo(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 15;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 7;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 43;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ReadCardOneBlock(RFdata rfdata, byte[] uid, byte add, byte block, byte blockNum) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = block;
            rfdata.SendData[15] = blockNum;
        } else {
            rfdata.SendData[1] = 9;
            rfdata.SendData[6] = block;
            rfdata.SendData[7] = blockNum;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 32;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ReadCardMultBlock(RFdata rfdata, byte[] uid, byte add, byte block, byte blockNum) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = block;
            rfdata.SendData[15] = blockNum;
        } else {
            rfdata.SendData[1] = 9;
            rfdata.SendData[6] = block;
            rfdata.SendData[7] = blockNum;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 35;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_WriteCardOneBlock(RFdata rfdata, byte[] uid, byte add, byte block, byte[] blockData) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        int i;
        if (add == 1) {
            rfdata.SendData[1] = 20;

            for(i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = block;

            for(i = 0; i < 4; ++i) {
                rfdata.SendData[15 + i] = blockData[i];
            }
        } else {
            rfdata.SendData[1] = 12;
            rfdata.SendData[6] = block;

            for(i = 0; i < 4; ++i) {
                rfdata.SendData[7 + i] = blockData[i];
            }
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 33;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_WriteCardMultBlock(RFdata rfdata, byte[] uid, byte add, byte block, byte blockNum, byte[] blockData) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        int k = blockNum + 1;
        int j;
        int i;
        if (add == 1) {
            rfdata.SendData[1] = (byte)(17 + (blockNum + 1) * 4);

            for(j = 0; j < 8; ++j) {
                rfdata.SendData[6 + j] = uid[j];
            }

            rfdata.SendData[14] = block;
            rfdata.SendData[15] = blockNum;

            for(j = 0; j < k; ++j) {
                for(i = 0; i < 4; ++i) {
                    rfdata.SendData[16 + i + j * 4] = blockData[i + j * 4];
                }
            }
        } else {
            rfdata.SendData[1] = (byte)(9 + (blockNum + 1) * 4);
            rfdata.SendData[6] = block;
            rfdata.SendData[7] = blockNum;

            for(j = 0; j < k; ++j) {
                for(i = 0; i < 4; ++i) {
                    rfdata.SendData[8 + i + j * 4] = blockData[i + j * 4];
                }
            }
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 36;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockCardOneBlock(RFdata rfdata, byte[] uid, byte add, byte block, byte blockNum) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = block;
            rfdata.SendData[15] = blockNum;
        } else {
            rfdata.SendData[1] = 9;
            rfdata.SendData[6] = block;
            rfdata.SendData[7] = blockNum;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 34;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_WriteOneTagDSFID(RFdata rfdata, byte[] uid, byte add, byte dsfid) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 16;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = dsfid;
        } else {
            rfdata.SendData[1] = 8;
            rfdata.SendData[6] = dsfid;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 41;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockOneTagDSFID(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 15;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 7;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 42;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_WriteOneTagAFI(RFdata rfdata, byte[] uid, byte add, byte afi) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 16;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }

            rfdata.SendData[14] = afi;
        } else {
            rfdata.SendData[1] = 8;
            rfdata.SendData[6] = afi;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 39;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockOneTagAFI(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 15;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[6 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 7;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -2;
        rfdata.SendData[4] = 40;
        rfdata.SendData[5] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_EnableOneTagEAS(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[8 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 9;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -19;
        rfdata.SendData[4] = -94;
        rfdata.SendData[5] = 4;
        rfdata.SendData[6] = 1;
        rfdata.SendData[7] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_BanOneTagEAS(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[8 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 9;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -19;
        rfdata.SendData[4] = -93;
        rfdata.SendData[5] = 4;
        rfdata.SendData[6] = 1;
        rfdata.SendData[7] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockOneTagEAS(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[8 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 9;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -19;
        rfdata.SendData[4] = -92;
        rfdata.SendData[5] = 4;
        rfdata.SendData[6] = 1;
        rfdata.SendData[7] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_CheckOneTagEAS(RFdata rfdata, byte[] uid, byte add) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        if (add == 1) {
            rfdata.SendData[1] = 17;

            for(int i = 0; i < 8; ++i) {
                rfdata.SendData[8 + i] = uid[i];
            }
        } else {
            rfdata.SendData[1] = 9;
        }

        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -19;
        rfdata.SendData[4] = -91;
        rfdata.SendData[5] = 4;
        rfdata.SendData[6] = 1;
        rfdata.SendData[7] = add;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_VerifyPassword(RFdata rfdata, byte[] uid, byte PassType, byte[] Pass) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 20;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -77;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
            if (i < 4) {
                rfdata.SendData[15 + i] = Pass[i];
            }
        }

        rfdata.SendData[14] = PassType;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ModifyPassword(RFdata rfdata, byte[] uid, byte PassType, byte[] Pass) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 20;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -76;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
            if (i < 4) {
                rfdata.SendData[15 + i] = Pass[i];
            }
        }

        rfdata.SendData[14] = PassType;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockPassword(RFdata rfdata, byte[] uid, byte PassType) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 16;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -75;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
        }

        rfdata.SendData[14] = PassType;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_EASAFIPasswordProtect(RFdata rfdata, byte[] uid, byte Type) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 16;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -90;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
        }

        rfdata.SendData[14] = Type;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_RunPageProtect(RFdata rfdata, byte[] uid, byte Number, byte condition) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 17;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -74;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
        }

        rfdata.SendData[14] = Number;
        rfdata.SendData[15] = condition;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_LockPageProtect(RFdata rfdata, byte[] uid, byte Number) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 16;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -64;
        rfdata.SendData[4] = -73;
        rfdata.SendData[5] = 1;

        for(int i = 0; i < 8; ++i) {
            rfdata.SendData[6 + i] = uid[i];
        }

        rfdata.SendData[14] = Number;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_OSFPDataSend(RFdata rfdata, byte[] data, int length) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = (byte)(7 + length);
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -18;
        rfdata.SendData[4] = -86;
        rfdata.SendData[5] = (byte)length;

        for(int i = 0; i < length; ++i) {
            rfdata.SendData[6 + i] = data[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_HardwareControl(RFdata rfdata, byte OpenType, byte HarType, byte Flag) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 9;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -90;
        rfdata.SendData[4] = 3;
        rfdata.SendData[5] = OpenType;
        rfdata.SendData[6] = HarType;
        rfdata.SendData[7] = Flag;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_OpenAnt_One(RFdata rfdata, byte Data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -90;
        rfdata.SendData[4] = 4;
        rfdata.SendData[5] = Data;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_InventoryTagISO14443A(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 1;
        rfdata.SendData[5] = 0;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443AKeyAuthentication(RFdata rfdata, byte[] uid, byte block, byte KeyType, byte[] key) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 18;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 5;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[11 + i] = key[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443AReadBlock(RFdata rfdata, byte[] uid, byte block, byte Num, byte KeyType, byte[] key) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 19;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 3;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = Num;
        rfdata.SendData[11] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[12 + i] = key[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443AWriteBlock(RFdata rfdata, byte[] uid, byte block, byte Num, byte KeyType, byte[] key, byte[] data) {
        rfdata.clear();
        int Length = 16 * Num;
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = (byte)(19 + Length);
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 4;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = Num;
        rfdata.SendData[11] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[12 + i] = key[i];
        }

        for(i = 0; i < Length; ++i) {
            rfdata.SendData[18 + i] = data[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443AFormatWriteBlock(RFdata rfdata, byte[] uid, byte block, byte KeyType, byte[] key, byte[] data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 22;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 6;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
            rfdata.SendData[17 + i] = data[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[11 + i] = key[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443ARechargeWriteBlock(RFdata rfdata, byte[] uid, byte block, byte Cmd, byte KeyType, byte[] key, byte[] data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 23;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 7;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
            rfdata.SendData[18 + i] = data[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = Cmd;
        rfdata.SendData[11] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[12 + i] = key[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ISO14443ABackWriteBlock(RFdata rfdata, byte[] uid, byte block, byte TAddr, byte KeyType, byte[] key) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 19;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -3;
        rfdata.SendData[4] = 8;

        int i;
        for(i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = uid[i];
        }

        rfdata.SendData[9] = block;
        rfdata.SendData[10] = TAddr;
        rfdata.SendData[11] = KeyType;

        for(i = 0; i < 6; ++i) {
            rfdata.SendData[12 + i] = key[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_NetScanDeviceInfo(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -53;
        rfdata.SendData[4] = 1;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_NetWriteDeviceInfo(RFdata rfdata, byte[] IPAdd, byte[] Mask, byte[] Gate) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 18;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -53;
        rfdata.SendData[4] = 2;

        for(int i = 0; i < 4; ++i) {
            rfdata.SendData[5 + i] = IPAdd[i];
            rfdata.SendData[9 + i] = Mask[i];
            rfdata.SendData[13 + i] = Gate[i];
        }
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_DoorTakeRecords(RFdata rfdata, short NumFlag, byte Flag) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 10;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 1;
        rfdata.SendData[5] = (byte)(NumFlag >> 8 & 255);
        rfdata.SendData[6] = (byte)(NumFlag & 255);
        rfdata.SendData[7] = Flag;
        rfdata.SendData[8] = 1;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_GetInOutInfo(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 2;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ClearInOutInfo(RFdata rfdata, byte data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 3;
        rfdata.SendData[5] = data;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_InOutExchange(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 4;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_SetSysTimer(RFdata rfdata, byte[] data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 12;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 5;

        for(int i = 0; i < 6; ++i) {
            rfdata.SendData[5 + i] = data[i];
        }

        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_GetSysTimer(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 6;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ClearAllRecordData(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 7;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_AllDoorAlarm(RFdata rfdata, byte data) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 7;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -37;
        rfdata.SendData[4] = 8;
        rfdata.SendData[5] = data;
        return this.crcAndSend_Receive(rfdata);
    }

    public boolean PC_ResartDowlnAPP(RFdata rfdata) {
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 6;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = -80;
        rfdata.SendData[4] = 1;
        return this.crcAndSend_Receive(rfdata);
    }

    /**
     * 字节数组转字符串
     * @param data 字节数组
     * @param len 要转换的长度
     * @param ishex 是否转换成16进制形式的字符串
     * @return
     */
    private String HexToStr(byte []data,int len,boolean ishex) {
        String strData="";
        for(int i = 0; i < len; i++) {
            if(ishex) {
                strData = strData + String.format("%02X ", data[i]);//十六进制
            }else{
                strData = strData + String.format("%d ", data[i]&0xff);//十进制
            }
        }
        return strData;
    }

    //AA 09 FF A6 03 01 01 00 8E 14  继电器闪烁
    //AA 09 FF A6 03 00 01 01 DB 5F  继电器打开
    //AA 09 FF A6 03 00 01 00 52 4E  继电器关闭
    public boolean relayControl(RFdata rfdata,int state){
        rfdata.clear();
        rfdata.SendData[0] = PC_SOF;
        rfdata.SendData[1] = 9;
        rfdata.SendData[2] = -1;
        rfdata.SendData[3] = (byte) 0xA6;
        rfdata.SendData[4] = 3;
//AA 09 A6 B0 03 01 01 00 44 FE
//AA 09 FF A6 03 01 01 00 8E 14
        switch (state){
            case 0:
                rfdata.SendData[5] = 1;
                rfdata.SendData[6] = 1;
                rfdata.SendData[7] = 0;
                rfdata.SendData[8] = (byte) 0x8E;
                break;
            case 1:
                rfdata.SendData[5] = 0;
                rfdata.SendData[6] = 1;
                rfdata.SendData[7] = 1;
                rfdata.SendData[8] = (byte) 0xDB;
                break;
            case 2:
                rfdata.SendData[5] = 0;
                rfdata.SendData[6] = 1;
                rfdata.SendData[7] = 0;
                rfdata.SendData[8] = (byte) 0x52;
                break;
        }

        return this.crcAndSend_Receive(rfdata);
    }
}
